home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / More Sprocket Examples 1.0 / DroneZone Sources / DZSpace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  3.6 KB  |  150 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZSpace.c
  3.  *
  4.  *    Contents:    Submits the spacejunk.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10.  
  11. #include <Quickdraw.h>
  12.  
  13. #include "QD3D.h"
  14. #include "QD3DGeometry.h"
  15. #include "QD3DShader.h"
  16. #include "QD3DView.h"
  17.  
  18. #include "DZSpace.h"
  19.  
  20.  
  21. #define SPACE_INTERVAL        2.0            // Grid cell size
  22. #define SPACE_CUBE            3            // Half side of the cube of cells
  23. #define SPACE_SPREAD1        0.0001        // Mulitplier to Random for line start
  24. #define SPACE_SPREAD2        0.00002        // Mulitplier to Random for line length
  25.  
  26.  
  27. static TQ3AttributeSet    gSpaceColor            = NULL;
  28.  
  29.  
  30. /* =============================================================================
  31.  *        Space_Init (external)
  32.  *
  33.  *    Initializes the space stuff.
  34.  * ========================================================================== */
  35. void Space_Init(
  36.     void)
  37. {
  38.     gSpaceColor = Q3AttributeSet_New();
  39. }
  40.  
  41.  
  42. /* =============================================================================
  43.  *        Space_Exit (external)
  44.  *
  45.  *    Prepares for exit.
  46.  * ========================================================================== */
  47. void Space_Exit(
  48.     void)
  49. {
  50.     if (gSpaceColor != NULL)
  51.     {
  52.         Q3Object_Dispose(gSpaceColor);
  53.         gSpaceColor = NULL;
  54.     }
  55. }
  56.  
  57.  
  58. /* =============================================================================
  59.  *        Space_Submit (external)
  60.  *
  61.  *    Submits all the 3D geometry of the spacejunk.
  62.  * ========================================================================== */
  63. void Space_Submit(
  64.     TQ3ViewObject        inView,
  65.     const TQ3Point3D*    inPosition,
  66.     const TQ3Vector3D*    inDirection)
  67. {
  68.     long                x;
  69.     long                y;
  70.     long                z;
  71.     long                loX;
  72.     long                loY;
  73.     long                loZ;
  74.     long                hiX;
  75.     long                hiY;
  76.     long                hiZ;
  77.     TQ3LineData            lineData;
  78.     float                px;
  79.     float                py;
  80.     float                pz;
  81.     TQ3ColorRGB            color;
  82.     
  83.     assert(inView != NULL);
  84.     assert(inPosition != NULL);
  85.     assert(inDirection != NULL);
  86.     
  87.     // Find the center grid cell
  88.     px = inPosition->x + inDirection->x*SPACE_INTERVAL*SPACE_CUBE;
  89.     py = inPosition->y + inDirection->y*SPACE_INTERVAL*SPACE_CUBE;
  90.     pz = inPosition->z + inDirection->z*SPACE_INTERVAL*SPACE_CUBE;
  91.     
  92.     x = (px + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  93.     y = (py + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  94.     z = (pz + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  95.     
  96.     // Find the range to cover
  97.     loX = x - SPACE_CUBE;
  98.     loY = y - SPACE_CUBE;
  99.     loZ = z - SPACE_CUBE;
  100.     
  101.     hiX = loX + 2*SPACE_CUBE;
  102.     hiY = loY + 2*SPACE_CUBE;
  103.     hiZ = loZ + 2*SPACE_CUBE;
  104.     
  105.     // Set up the line data
  106.     lineData.vertices[0].attributeSet    = NULL;
  107.     lineData.vertices[1].attributeSet    = NULL;
  108.     lineData.lineAttributeSet            = gSpaceColor;
  109.     
  110.     // Do the cube
  111.     for (x = loX; x <= hiX; x++)
  112.     {
  113.         px = (x+0.5)*SPACE_INTERVAL;
  114.         
  115.         for (y = loY; y <= hiY; y++)
  116.         {
  117.             py = (y+0.5)*SPACE_INTERVAL;
  118.             
  119.             for (z = loZ; z <= hiZ; z++)
  120.             {
  121.                 pz = (z+0.5)*SPACE_INTERVAL;
  122.                 
  123.                 // Make Random() repeatable based on cell index
  124.                 qd.randSeed = x^y^z;
  125.                 
  126.                 // Set up the endpoints
  127.                 lineData.vertices[0].point.x = px + Random()*SPACE_SPREAD1;
  128.                 lineData.vertices[0].point.y = py + Random()*SPACE_SPREAD1;
  129.                 lineData.vertices[0].point.z = pz + Random()*SPACE_SPREAD1;
  130.                 
  131.                 lineData.vertices[1].point.x = lineData.vertices[0].point.x + Random()*SPACE_SPREAD2;
  132.                 lineData.vertices[1].point.y = lineData.vertices[0].point.y + Random()*SPACE_SPREAD2;
  133.                 lineData.vertices[1].point.z = lineData.vertices[0].point.z + Random()*SPACE_SPREAD2;
  134.                 
  135.                 // Set up the line color -- blend between purple and green
  136.                 color.b = ((unsigned short) Random()) * (1.0/65535.0);
  137.                 color.r = 0.4*color.b;
  138.                 color.g = 0.8*(1.0-color.b);
  139.                 
  140.                 Q3AttributeSet_Add(gSpaceColor, kQ3AttributeTypeDiffuseColor, &color);
  141.                 
  142.                 // Submit the line
  143.                 Q3Line_Submit(&lineData, inView);
  144.             }
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150.